package com.controller;


import com.alibaba.fastjson.JSONObject;
import com.pojo.Users;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource(name="userService")
    private UserService userService;
    /**
     * 根据id查询
     */
    @RequestMapping(value="/login")
    public void login(HttpServletRequest request, HttpServletResponse response){
        try{
            String username =request.getParameter("username");
            String password=request.getParameter("password");
            boolean result;
            Users user=userService.login(username,password);
            if(user==null)
                result=false;
            else {
                result=true;
            }
            ReturnResult(response,result,user);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    @RequestMapping(value="/signup")
    public void signup(HttpServletRequest request,Users users,HttpServletResponse response) throws Exception {
        try {
            SetUsers(users);
           boolean result=userService.signup(users);
           ReturnResult(response,result);

        }catch (Exception e){
            e.printStackTrace();
            PrintWriter writer=response.getWriter();
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("code",0);
            response.setContentType("text/html;charset=UTF-8");
            writer.print(jsonObject);
            writer.close();
        }
    }
    private void ReturnResult(HttpServletResponse response,boolean result,Users user){
        try {
            PrintWriter writer=response.getWriter();
            JSONObject jsonObject=new JSONObject();
            if(result){
                jsonObject.put("code",1);
                System.out.println(user.getId());
                jsonObject.put("username",user.getUsername());
                jsonObject.put("userid",user.getId());
            }
            else
                jsonObject.put("code",0);
            response.setContentType("text/html;charset=UTF-8");
            writer.print(jsonObject);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void ReturnResult(HttpServletResponse response,boolean result){
        try {
            PrintWriter writer=response.getWriter();
            JSONObject jsonObject=new JSONObject();
            if(result){
                jsonObject.put("code",1);
            }
            else
                jsonObject.put("code",0);
            response.setContentType("text/html;charset=UTF-8");
            writer.print(jsonObject);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void SetUsers(Users user){
        user.setId(userService.selectMaxId()+1);
        user.setidNUll();
    }
}
